﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Kindohm.Life.Library
{
    public class Herbivore : Creature
    {
        Plant plannedFood;

        public static new Herbivore Create()
        {
            Herbivore item = new Herbivore();
            Creature.Initialize(item, true);
            return item;
        }

        protected override void ProcessFreeWill()
        {

            if (this.plannedFood != null)
            {
                this.GoForPlant(this.plannedFood);
                return;
            }

            if (this.plannedMate != null)
            {
                this.GoForMate(this.plannedMate);
                return;
            }

            if (!this.IsHorny & !this.IsHungry)
            {
                //nothin to do
                this.Walk();
                return;
            }

            Creature idealMate = null;

            if (this.IsHorny)
            {
                idealMate = this.FindIdealMate();
            }

            Plant idealPlant = null;
            if (this.IsHungry)
            {
                PlantFinder finder = new PlantFinder();
                idealPlant = finder.FindIdealPlant(this.Board, this.Position, this.Sight);
            }

            if (idealMate != null & idealPlant == null)
            {
               this.GoForMate(idealMate);
               return;
            }

            if (idealMate == null & idealPlant != null)
            {
                this.GoForPlant(idealPlant);
                return;
            }

            if (idealPlant != null & idealMate != null)
            {
                int plantDistance = Point.Distance(this.Position, idealPlant.Position);
                int mateDistance = Point.Distance(this.Position, idealMate.Position);

                if (plantDistance <= mateDistance)
                {
                    this.GoForPlant(idealPlant);
                    return;
                }
                else
                {
                    this.GoForMate(idealMate);
                    return;
                }
            }

            //last option
            this.Walk();

            base.ProcessFreeWill();
        }

        public Plant PlannedFood
        {
            get { return this.plannedFood; }
        }
        

        void GoForPlant(Plant plant)
        {
            if (plant.Nutrition == 0)
            {
                //someone else got there first
                this.plannedFood = null;
                return;
            }

            if (Board.AreAdjacent(plant.Position, this.Position))
            {
                this.Eat(plant);
                return;
            }
            else
            {
                this.plannedFood = plant;
                this.WalkTo(plant.Position);
                return;
            }
        }

        public override void Eat(IEdible target)
        {
            if (target is Plant == false)
            {
                throw new EatException("The desired food was not a plant.");
            }
            this.plannedFood = null;
            base.Eat(target);
        }

        protected override Creature FindIdealMate()
        {
            if (this.Board == null)
            {
                return null;
            }

            MateFinder finder = new MateFinder();
            Sex targetSex = Sex.Male;
            if (this.Sex == Sex.Male)
            {
                targetSex = Sex.Female;
            }
            return finder.FindIdealMate(this.Board, this.Position, this.Sight, targetSex, typeof(Herbivore));
        }

    }
}
